package com.probiz.estoresa.other.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.other.Tag;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.other.service.TagManager;
import com.probiz.estore.webapp.util.RequestUtil;

public class TagController extends GenericStoreAdminController<Tag> {
    private TagManager tagManager = null;

    public void setTagManager(TagManager inMgr) {
        this.tagManager = inMgr;
    }

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#getEntityName(java.lang.Object)
	 */
	@Override
	protected String getEntityName(Tag entity) {
		return entity.getTagName();
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.BaseController#initController()
	 */
	@Override
	protected void initController() throws Exception {
		mgr = tagManager;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.controller.GenericController#onSave(javax.servlet.http.HttpServletRequest,
	 *      java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected void preSave(HttpServletRequest request, Tag entity, BindException errors) {
	}
	
	public ModelAndView defaultAction(HttpServletRequest request,	HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("other/tagList");
		List<Tag> tagList = tagManager.listByProperty("module", Short.valueOf(RequestUtil.getParameterNullSafe(request, "module")));
		mv.addObject("tagList", tagList);
		return mv;
	}

	public ModelAndView add(HttpServletRequest request,	HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Tag tag = new Tag();
		tag.setModule(Short.valueOf(RequestUtil.getParameterNullSafe(request, "module")));
		tag.setTagName(RequestUtil.getParameterNullSafe(request, "tagName").trim());
		Map<String, Object> returnMsgs =  tagManager.saveFully(tag);
		data.putAll(returnMsgs);
		data.put("tagId", tag.getTagId());
		data.put("tagName", tag.getTagName());
		data.put("module", tag.getModule());
		return getModelAndAjaxJsonView(data);
	}
	
	public ModelAndView rename(HttpServletRequest request,	HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Tag tag = this.formBackingObject(request);
		tag.setTagName(RequestUtil.getParameterNullSafe(request, "tagName").trim());
		Map<String, Object> returnMsgs = tagManager.saveFully(tag);
		data.putAll(returnMsgs);
		data.put("tagId", tag.getTagId());
		data.put("tagName", tag.getTagName());
		data.put("module", tag.getModule());
		return getModelAndAjaxJsonView(data);
	}
	public ModelAndView delete(HttpServletRequest request,	HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Tag tag = this.formBackingObject(request);
		tagManager.deleteById(tag.getTagId());
		data.put("tagId", tag.getTagId());
		data.put("tagName", tag.getTagName());
		data.put("module", tag.getModule());
		return getModelAndAjaxJsonView(data);
	}
}
